import { Application } from "mydog";
import { Dictionary, IUidSid } from "../../definition/interface";
import { DoudizhuGame } from "./doudizhuGame";

class DoudizhuManager {

  private static _instance: DoudizhuManager | undefined = undefined;
  public static getInstance(): DoudizhuManager {
    if (!this._instance) {
      this._instance = new DoudizhuManager();
    }
    return this._instance;
  }

  private constructor() { }

  private _app: Application = null as any;
  public init(app: Application) {
    this._app = app;
  }

  private _doudizhuMap: Dictionary<DoudizhuGame> = {};

  public onInitGame(roomId: number, uidsid: Array<IUidSid>) {
    this.addGame(roomId, uidsid);
    return true;
  }

  public onStartGame(roomId: number) {
    const game = this._doudizhuMap[roomId];
    if (!!game) {
      game.onStart();
    }
  }

  public onEndGame(roomId: number) {

  }

  public onQiangdizhu(roomId: number, uid: number, score: number) {
    this._doudizhuMap[roomId]?.onQiangdizhu(uid, score);
  }

  // 添加对局
  private addGame(roomId: number, uidsid: Array<IUidSid>) {
    const game = new DoudizhuGame(this._app, roomId, uidsid);
    this._doudizhuMap[roomId] = game;
  }

}

export default DoudizhuManager.getInstance();
